package cn.xiaopengstack.trigger.http.advice;

import cn.xiaopengstack.types.enums.ResponseCode;
import cn.xiaopengstack.types.exception.AppException;
import cn.xiaopengstack.types.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jiangyangang
 */
@ControllerAdvice
@Slf4j
public class ControllerExceptionAdvice {

    @ExceptionHandler(value = {AppException.class})
    @ResponseBody
    public Response<String> appExceptionHandler(AppException e, Model model) {
        log.error("业务异常", e);
        return Response.error(null, ResponseCode.getResp(e.getCode()).orElse(ResponseCode.UN_ERROR));
    }
}
